package fy.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.commons.logging.LogFactory;

import fy.config.DBConfigBean;

public class DBUtil {

	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(DBUtil.class);

	/**
	 * 
	 * @param driverClassName
	 * @param url
	 * @param userName
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public static Connection connect(String driverClassName, String url,
			String userName, String password) throws Exception {

		Class.forName(driverClassName);
		DriverManager.setLoginTimeout(60);
		return DriverManager.getConnection(url, userName, password);
	}

	public static Connection connect(DBConfigBean bean) throws Exception {

		Class.forName(bean.getDriverClassName());
		DriverManager.setLoginTimeout(60);
		return DriverManager.getConnection(bean.getUrl(), bean.getUser(),
				bean.getPassword());
	}

	public static void rollback(Connection conn) {

		try {
			if (conn != null && !conn.isClosed()) {
				conn.rollback();
				conn.setAutoCommit(true);
			}
		} catch (Exception e) {
			log.error("数据库操作回滚出错", e);
		}

	}

	public static void setAutoCommit(Connection conn, boolean autoCommit) {
		try {

			if (conn != null && !conn.isClosed()) {
				conn.setAutoCommit(autoCommit);
			}

		} catch (Exception e) {
			log.error("setAutoCommit出错", e);
		}
	};

	public static void close(Statement st) {
		try {
			if (st != null) {
				st.close();
				st = null;
			}
		} catch (Exception e) {
			log.error("关闭PreparedStatement 出错", e);
		}
	}

	public static void close(PreparedStatement st) {
		try {
			if (st != null) {
				try {
					st.clearBatch();
				} catch (Exception e1) {
				}
				st.close();
				st = null;
			}
		} catch (Exception e) {
			log.error("关闭PreparedStatement 出错", e);
		}
	}

	public static void close(ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
		} catch (Exception e) {
			log.error("关闭ResultSet出错", e);
		}
	}

	public static void close(Connection conn) {
		try {
			if (conn != null && !conn.isClosed()) {
				conn.close();
				conn = null;
			}

		} catch (Exception e) {
			log.error("关闭Connection出错", e);
		}
	}

	public static void close(ResultSet rs, Statement st, Connection conn) {
		close(rs);
		close(st);
		close(conn);
	}

	public static void close(ResultSet rs, PreparedStatement st, Connection conn) {
		close(rs);
		close(st);
		close(conn);
	}

	public static void close(ResultSet rs, Statement st) {
		close(rs);
		close(st);
	}

	public static void close(ResultSet rs, PreparedStatement pst) {
		close(rs);
		close(pst);
	}

	public static void close(Statement st, Connection conn) {
		close(st);
		close(conn);
	}

	public static void close(PreparedStatement pst, Connection conn) {
		close(pst);
		close(conn);
	}

	public static boolean checkDBConnect(String driver, String url,
			String userName, String password) {
		boolean success = false;
		try {
			close(connect(driver, url, userName, password));
			success = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return success;
	}

	public static void main(String[] args) {
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		try {
			conn = connect("oracle.jdbc.driver.OracleDriver",
					"jdbc:oracle:oci:@192.168.244.60:1521:lsj", "lsj", "lsj");
			stat = conn.createStatement();
			rs = stat
					.executeQuery("select *  from bd_defdoc where (pk_defdoclist = '0001ORGCORP000000002' and      (pk_corp = '0001' or pk_corp = '0001'))  and (sealflag is null or sealflag <> 'Y') order by doccode");

			System.out.println(rs.getMetaData().getColumnCount());

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(rs);
			close(stat);
			close(conn);
		}
	}
}
